package {{.ModuleName}}

import (
	"dims/entity"
	"dims/routers/validator"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 条件分页查询项目信息
func {{.BusinessName}}PageHandler(c *gin.Context) (err error) {
	req := &entity.{{.ClassName}}PageReq{}
	if err = c.ShouldBind(req); err == nil {
		list, count, err := svc.{{.ClassName}}QueryPage(req)
		if err != nil {
			return err
		}
		c.JSON(http.StatusOK, &entity.StandardResult{
			Code: entity.StandardResultSuccess,
			Data: &entity.PageResult{
				Total: count,
				List:  list,
			},
		})
	}
	return
}

func {{.BusinessName}}SaveHandler(c *gin.Context) (err error) {
	req := &entity.{{.ClassName}}{}
	if err = c.ShouldBind(req); err == nil {
		//验证字段
		errs := validator.ValidateStruct(req)
		if errs != nil {
			return &entity.BusinessError{Message: errs.Error()}
		}

		id, err := svc.{{.ClassName}}Save(*req)
		if err != nil {
			return err
		}

		c.JSON(http.StatusOK, &entity.StandardResult{
			Code:    entity.StandardResultSuccess,
			Message: "保存成功！",
			Data:    id,
		})
	}
	return
}

func {{.BusinessName}}UpdateHandler(c *gin.Context) (err error) {
	req := &entity.{{.ClassName}}{}
	if err = c.ShouldBind(req); err == nil {
		//验证字段
		errs := validator.ValidateStruct(req)
		if errs != nil {
			return &entity.BusinessError{Message: errs.Error()}
		}

		id, err := svc.{{.ClassName}}Update(*req)
		if err != nil {
			return err
		}

		c.JSON(http.StatusOK, &entity.StandardResult{
			Code:    entity.StandardResultSuccess,
			Message: "更新成功！",
			Data:    id,
		})
	}
	return
}

func {{.BusinessName}}DeleteHandler(c *gin.Context) (err error) {
	uid, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		return err
	}
	err = svc.{{.ClassName}}Delete(uid)
	if err != nil {
		return err
	}
	c.JSON(http.StatusOK, &entity.StandardResult{
		Code:    entity.StandardResultSuccess,
		Message: "删除成功！",
	})
	return
}

func {{.BusinessName}}LoadHandler(c *gin.Context) (err error) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		return err
	}
	result := svc.{{.ClassName}}LoadById(id)
	c.JSON(http.StatusOK, &entity.StandardResult{
		Code:    entity.StandardResultSuccess,
		Message: "查询成功！",
	    Data: result,
	})
	return
}

